<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //一、根据索引获取字符串的值
      //1、str.chartAt(索引值);
      var str = '123456'
      var str4 = str.charAt(4) //取得str索引为4的值
      console.log(str4)
      console.log(str[4]) //取得ste索引为4的值
      str[4] = 'h' //不能通过下标设置字符串的值
      //二、根据字符串的索引值获取字符串对应ASCII编码
      var str = 'abcdefg'
      var res = str.charCodeAt(2)
      console.log(str, `c的编码是：${res}`)
      //三、根据某个字符 把字符串切割成数组 split(字符)
      var str = 'a*b*c*d'
      var arr = str.split('*')
      console.log(arr)
      //四、截取字符串
      //1、substr(开始的索引,截取的数量（不指定默认到结尾）)
      var str = 'abcdefg'
      var res = str.substr(2, 3) //cde
      var res1 = str.substr(2) //cdefg  没有第二个参数截取到末尾
      console.log(res, res1)
      //2、substring(开始的索引,结束的索引)  包前不包后
      var str = 'abcdefg'
      var res = str.substring(2, 4) // cd
      console.log(res)

      //3、slice(开始的索引,结束的索引) 包前不包后
      var str = 'abcdefg'
      var res = str.slice(1, 4) //bcd
      console.log(res)

      //五、字符串里的英文转大写
      var str = 'abcDefG'
      var res = str.toUpperCase()
      console.log(res)
      //六、字符串的英文转小写
      var str = 'abcDefG'
      var res = str.toLowerCase()
      console.log(res)
      //降低函数的易错率
      function justifyColor(str) {
        if (str.toLowerCase() == 'red' && str.toUpperCase() == 'RED') {
          console.log('此为红色')
        } else {
          console.log('未知颜色')
        }
      }
      justifyColor('REd')
      //七、替换字符串
      //1、 replace('替换的值','替换的新值')
      var str = 'asdasdXXXddwedwedewdXXXdededXXX'
      var str1 = str
        .replace('XXX', '**')
        .replace('XXX', '**')
        .replace('XXX', '**')
      console.log(str1)
      //2、replaceAll('替换的值','替换的新值')
      var str2 = str.replaceAll('XXX', '**')
      console.log(str2)
      //3、使用正则；
      var reg = /XXX/g
      var res = str.replaceAll(reg, '**')
      var res1 = str.replace(reg, '**')
      console.log(res)
      console.log(res1)
      //4、通过while循环替换字符串
      while (str.indexOf('XXX') != -1) {
        str = str.replace('XXX', '**')
      }
      //八、查找字符串的索引值 indexOf(查找的值)
      var str = 'abcdefg'
      var res = str.indexOf('g')
      console.log(res) //6
      var res = str.indexOf('w')
      console.log(res) //找不到返回值为-1
    </script>
  </body>
</html>
